package com.gjy.test.other;

import com.gjy.util.ReflectUtil;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-01-18 14:18:29
 */
public class T7 {

    List<String> list = new ArrayList<>();

    @Test
    public void test6() throws Exception {
        Class<T7> clazz = T7.class;
        Field field = clazz.getDeclaredField("list");
        Type type = field.getGenericType();
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Type[] arguments = parameterizedType.getActualTypeArguments();
            for (Type argument : arguments) {
                Class<?> clz = (Class<?>) argument;
                System.out.println("clz = " + clz);
            }
        }

        System.out.println("list.getClass().getComponentType() = " + list.getClass().getComponentType());
    }

    @Test
    public void test5() {
        System.out.println("tran(\"\") = " + tran2(""));
//
        System.out.println("tran(null) = " + tran2(null));
    }

    private static BigDecimal tran(String a) {
        BigDecimal bigDecimal = new BigDecimal(a);
        System.out.println("bigDecimal = " + bigDecimal);
        return bigDecimal;
    }

    private static BigDecimal tran2(String a) {
        BigDecimal bigDecimal = BigDecimal.valueOf(Double.parseDouble(a));
        System.out.println("bigDecimal = " + bigDecimal);
        return bigDecimal;
    }

    @Test
    public void test4() {
        String a = "2023-02-06";
        System.out.println("a.substring(0,4) = " + a.substring(0, 4));
        System.out.println("a.substring(5,7) = " + a.substring(5, 7));

        System.out.println("Integer.parseInt(a.substring(5, 7)) = " + Integer.parseInt(a.substring(5, 7)));
    }

    @Test
    public void test2() throws Exception {
        Class<ReflectUtil> clazz = ReflectUtil.class;
        Field field = clazz.getDeclaredField("NAME");
        System.out.println("field.getModifiers() = " + field.getModifiers());
        System.out.println(Modifier.toString(field.getModifiers()));
    }

    @Test
    public void test1() {
        Class<T1> t1 = T1.class;

        System.out.println("t1.getName() = " + t1.getName());
        System.out.println("t1.getSimpleName() = " + t1.getSimpleName());
        System.out.println("t1.getCanonicalName() = " + t1.getCanonicalName());
        System.out.println("t1.getPackage() = " + t1.getPackage());
    }

    @Test
    public void test3() {
        for (int i = 0; i < 20; i++) {
            System.out.println("ThreadLocalRandom.current().nextInt(5) = " +
                    ThreadLocalRandom.current().nextInt(5));
        }
    }
}
